<template>
  <div class="top-bar">
    <div class="bar-left" @click="toIndex">Geek</div>
    <div class="bar-right">
      <div
        class="menu"
        v-for="item in menuList"
        :key="item.notebookName"
        @click="routeToNotebook(item.nid)"
      >
        {{ item.notebookName }}
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { reactive, onMounted, toRefs, defineComponent } from "vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import { getNotebookList } from "@/api/index";

export default defineComponent({
  setup() {
    const router = useRouter();
    const store = useStore();
    const reactiveData = reactive({
      menuList: [] as Array<{
        notebookName: string;
        nid: string;
      }>,
    });

    function toIndex(): void {
      router.push({ name: "Home" });
    }

    function routeToNotebook(nid: number): void {
      store.commit("viewNotebook", nid);
      router.push({
        name: "ArticlesPage",
      });
    }

    onMounted(() => {
      getNotebookList().then((res: any) => {
        console.log(res);
        const { code, data } = res;
        if (code === 200) {
          reactiveData.menuList.push(...data.notebookList);
        }
      });
    });

    return {
      ...toRefs(reactiveData),
      toIndex,
      routeToNotebook,
    };
  },
});
</script>

<style lang="scss" scoped>
.top-bar {
  position: fixed;
  top: 0;
  display: flex;
  width: 100%;
  height: 50px;
  color: #fff;
  transition: all 0.1s linear;
  opacity: 0.9;

  &:hover {
    box-shadow: 0 0 20px rgb(39, 23, 23);
  }

  .bar-left {
    box-sizing: border-box;
    padding-left: 20px;
    width: 50%;
    font-size: 20px;
    line-height: 50px;

    &:hover {
      cursor: pointer;
    }
  }

  .bar-right {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    width: 50%;

    .menu {
      margin: 0 10px;
      height: 45px;
      width: 20%;
      line-height: 45px;
      text-align: center;
      transition: all 0.1s linear;

      &:hover {
        background-color: rgba(14, 14, 14, 0.6);
        box-shadow: 0px 0px 30px 0px rgb(39, 23, 23);
        cursor: pointer;
      }

      .child-menu {
        position: relative;
        box-sizing: border-box;
        overflow: hidden;
        padding: 0 10px;
        height: 45px;
        width: 100%;
        text-align: center;
        text-overflow: ellipsis;
        white-space: nowrap;
        transition: all 0.1s linear;

        &:hover {
          background-color: rgba(14, 14, 14, 0.4);
          box-shadow: 0px 5px 10px 0px rgb(39, 23, 23);
          cursor: pointer;
        }
      }
    }
  }
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s;
}

.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
  opacity: 0;
}
</style>
